package problems.contest;

import java.util.Arrays;

/**
 * @author dubulingbo, 2023/7/23 9:08.
 */
public class BCT109 {
    /**
     * T1|6930. 检查数组是否是好的
     * <p>https://leetcode.cn/problems/check-if-array-is-good/</p>
     */
    public boolean isGood(int[] nums) {
        if (nums.length < 2) return false;
        int sz = nums.length;
        int n = sz - 1;
        boolean[] f = new boolean[sz];

        for (int num : nums) {
            if (num < 1 || num > n || (num != n && f[num])) return false;
            f[num] = true;
        }

        return true;
    }


    /**
     * T2|6926. 将字符串中的元音字母排序
     * <p>https://leetcode.cn/problems/sort-vowels-in-a-string/</p>
     */
    public String sortVowels(String s) {
        char[] ret = s.toCharArray();

        // 提取元音字符
        StringBuilder sb = new StringBuilder();
        for (char c : ret) {
            if (check(c)) sb.append(c);
        }

        // 元音字符个数小于2，直接返回原字符串
        if (sb.length() < 2) return s;

        char[] chs = sb.toString().toCharArray();
        // 非降序排序
        Arrays.sort(chs);

        // 替换元音字符
        int j = 0;
        for (int i = 0; i < ret.length; ++i) {
            if (check(ret[i])) ret[i] = chs[j++];
        }

        return new String(ret);
    }

    private boolean check(char c) {
        c = Character.toLowerCase(c);
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }

}
